import 'dart:io';

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:native_dart_package/loading/loading_util.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/pages/mine/about_us/about_us_controller.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/constants/store_keys.dart';

import '../../../components/log_utils.dart';

class AboutUsPage extends GetView<AboutUsController> {
  AboutUsPage({Key? key}) : super(key: key);
  @override
  final controller = Get.put(AboutUsController());
  final state = Get.find<AboutUsController>().state;

  Widget _createLogo() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Container(
          margin: const EdgeInsets.only(top: 48),
          child: Image.asset(
            ImageAssets.logo,
            width: 58.w,
            height: 58.w,
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(
            top: 24,
            bottom: 6,
          ),
          child: Text(
            ResKey.company.tr,
            style: ytTextStyle(
              color: Colour.mainTextColor,
              fontSize: 18,
              fontWeight: Platform.isIOS?FontWeight.w500:FontWeight.w600,
            ),
          ),
        ),
        Container(
          margin: const EdgeInsets.only(bottom: 33),
          child: GetBuilder(
            init: controller,
            builder: (controller) {
              return Text(
                "V${controller.aboutInfoState.version ?? '1.0.0'}",
                style: TextStyle(
                  color: Colour.mainTextColor,
                  fontSize: 14.sp,
                ),
              );
            },
          ),
        ),
      ],
    );
  }

  Widget _createItem(int index) {
    return Column(
      children: [
        ListTile(
          dense: true,
          minLeadingWidth: 0,
          title: Text(
            state.aboutUsLabelList[index].title!.tr,
            style: TextStyle(
              color: Colour.mainTextColor,
              fontSize: 16.sp,
            ),
          ),
          trailing: Image.asset(
            ImageAssets.rightArrow,
          ),
          onTap: () {
            if (index == 1) {
              controller.clickCount++;
              if (controller.clickCount >= 10) {
                Get.toNamed(
                  RouterPath.noPermission,
                );
                GetStorage storage = GetStorage();
                String textToCopy = storage.read(StoreKeys.pushDeviceToken) ?? "token is null";
                Clipboard.setData(ClipboardData(text: textToCopy));
                ScaffoldMessenger.of(Get.context!).showSnackBar(
                  const SnackBar(content: Text('Text copied!')),
                );
                controller.clickCount = 0; // Reset the click count
              }
            }else
            if (index == 2) {
              Get.toNamed(
                RouterPath.privatePolicy,
              );
            } if (index == 3) {
              if(controller.isNeedToUpdate){
                controller.goToUpdate();
              }else{
              BrnToast.show(ResKey.isLatestVersion.tr, Get.context!,duration:const Duration(milliseconds: 1000));
              }
            }
          },
        ),
        _createDivider()
      ],
    );
  }

  Widget _createUpdateBtn() {
    return Container(
      margin: EdgeInsets.only(
        top: 120.h,
        left: 16.w,
        right: 16.w,
      ),
      child: BrnBigMainButton(
        title: controller.updateString,
        onTap: () => controller.goToUpdate(),
      ),
    );
  }

  Widget _createDivider() {
    return Container(
      margin: const EdgeInsets.only(left: 16,right: 16),
      height: 1,
      color: Colour.dividerColor,
    );
  }

  Widget _createList() {
    final length = state.aboutUsLabelList.length;
    return ListView.builder(
      physics: const NeverScrollableScrollPhysics(),
      itemBuilder: (context, index) {
        if (index == 0) {
          return _createLogo();
        }
        // else if (index == length) {
        //   return _createUpdateBtn();
        // }
        return _createItem(index);
      },
      // itemCount: length + 1,
      itemCount: length,
      // separatorBuilder: (BuildContext context, int index) => _createDivider(),
    );
  }

  Widget _createCopyright() {
    return Container(
      margin: EdgeInsets.only(bottom: 50.h),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Text(
            ResKey.copyright.tr,
            textAlign: TextAlign.center,
            style: const TextStyle(
              color: Colour.lightTextColor,
            ),
          ),
          Text(
            ResKey.copyrightCompany.tr,
            textAlign: TextAlign.center,
            style: const TextStyle(
              color: Colour.lightTextColor,
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AboutUsController>(
      builder: (_) {
        return Scaffold(
          backgroundColor: Colour.white,
          appBar: AppBarWidget.defaultAppBar(ResKey.aboutUs.tr),
          body: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Expanded(
                child: _createList(),
              ),
              // _createCopyright(),
            ],
          ),
        );
      },
    );
  }
}
